package com.home.common;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileOperation {

	public static String path = "d:/img/";

	public static String save(String fileName,InputStream in) throws IOException {
		String newFileName=System.currentTimeMillis()+getSuffixName(fileName);
		File dir=new File(path);
		if(!dir.exists()){
			dir.mkdir();
		}
		File file=new File(path+newFileName);
		FileOutputStream fis=new FileOutputStream(file);
		BufferedInputStream input=new BufferedInputStream(in);
		BufferedOutputStream bos=new BufferedOutputStream(fis);
		byte[] b=new byte[input.available()];
			input.read(b);
		    bos.write(b);
		in.close();
		input.close();
		bos.close();
		fis.close();
		return newFileName;
	}

	
	
	private static String getSuffixName(String fileName){
		if(fileName==null||fileName.equals("")){
			return "";
		}
		return fileName.substring(fileName.lastIndexOf("."),fileName.length());
	}
	
	
	public static void delete(String fileName) {
		File file=new File(path+fileName);
		if(file.exists()){
			file.delete();
		}
		
	}

}
